package com.example.answer_staff;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.answer_staff.adapter.HomeMenuAdapter;
import com.gaosai.staff.utils.CommonUtils;
import com.gaosai.staff.utils.SPUtil;

/**
 * 基础父类 activity  可以放一些公共的东西  比如公共的头布局等等。
 * @author willis
 */
public class BaseActivity  extends FragmentActivity{
	/**内容布局**/
	protected LinearLayout content_view ;

	/**侧滑按钮*/
	protected ImageView slide_menu ;

	/**过滤按钮**/
	protected ImageView fliter ;

	/**右按钮*/
	protected ImageView newsRoom_search ;

	//title标题
	private TextView page_title ;

	//sideMENU
	protected DrawerLayout mDrawerLayout ;

	private ArrayList<String> mMenuStrList;

	/**侧滑菜单项ListView**/
	private ListView mDrawerList ;

	/**侧滑菜单项**/
	private String[] mMenuArray;

	/**菜单适配器**/
	private HomeMenuAdapter mMenuAdapter;

	/**包含菜单的布局**/
	private RelativeLayout slideMenu_rl ;

	/**包含搜索title的布局**/
	private RelativeLayout search_rl ;

	/**普通的titleBar布局**/
	private RelativeLayout common_rl ;

	private LinearLayout logoff;

	private TextView welcome;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_base) ;
		initView() ;
	}
	private void initView() {
		welcome=(TextView)findViewById(R.id.welcome);
		welcome.setText("Welcome ,"+SPUtil.getValue(getApplicationContext(), SPUtil.USERNAME));
		logoff=(LinearLayout)findViewById(R.id.logoff);
		logoff.setOnClickListener(new LogOffListeners());
		mDrawerLayout = (DrawerLayout) this.findViewById(R.id.drawer_layout) ;
		content_view = (LinearLayout) this.findViewById(R.id.page_content_lay) ;
		slide_menu = (ImageView) this.findViewById(R.id.slide_menu) ;
		fliter = (ImageView) this.findViewById(R.id.fliter) ;
		newsRoom_search = (ImageView) this.findViewById(R.id.newsRoom_search) ;
		page_title = (TextView) this.findViewById(R.id.title_tv) ;

		mDrawerList = (ListView) this.findViewById(R.id.left_drawer) ;
		mMenuArray = getResources().getStringArray(R.array.mune_array);
		mMenuStrList = new ArrayList<String>();
		for (int i = 0; i < mMenuArray.length; i++) {
			mMenuStrList.add(mMenuArray[i]);
		}
		mMenuAdapter = new HomeMenuAdapter(this,mMenuStrList);
		mDrawerList.setAdapter(mMenuAdapter);
		mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
		slideMenu_rl = (RelativeLayout) this.findViewById(R.id.slideMenu_rl) ;
		common_rl = (RelativeLayout) this.findViewById(R.id.common_rl) ;
		search_rl = (RelativeLayout) this.findViewById(R.id.search_rl) ;
	}
	class LogOffListeners implements OnClickListener{
		@Override
		public void onClick(View arg0) {
			Intent inten=new Intent(getApplicationContext(),LoginActivity.class);
			inten.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(inten);
			finish();
		}
	}
	//	106.187.93.119:8087

	/**
	 * 设置标题栏页面的名字
	 * @Description:TODO
	 * @param pageName
	 */
	protected void setTitleName(String pageName) {
		page_title.setText(pageName);
	}

	/**
	 * 显示Title左按钮
	 * @Description:TODO
	 * @param image  按钮背景图片
	 * @param listener  按钮的点击监听器
	 */
	protected void showtitlebtnLeftWithImg(int image, OnClickListener listener) {
		//left_btn.setBackgroundDrawable(getResources().getDrawable(image));
		slide_menu.setImageResource(image) ;
		slide_menu.setOnClickListener(listener);
		slide_menu.setVisibility(View.VISIBLE);
	}

	/**
	 * 默认显示左按钮的
	 * @param image
	 */
	protected void showtitleBtnLeftWidthImgDefaule(int image){
		slide_menu.setImageResource(image) ;
		slide_menu.setOnClickListener(leftDefaultOnClickListener);
		slide_menu.setVisibility(View.VISIBLE);
	}
	/**
	 * 显示左按钮和右按钮
	 * @param leftImage
	 * @param rightImg
	 * @param RightListener
	 */
	protected void showTitleBtnLeftAndRightImg(int leftImage,int rightImg,OnClickListener rightListener){
		if(leftImage !=0){
			slide_menu.setImageResource(leftImage) ;
			slide_menu.setOnClickListener(leftDefaultOnClickListener);
			slide_menu.setVisibility(View.VISIBLE);
		}
		if(rightImg !=0){
			newsRoom_search.setImageResource(rightImg) ;
			newsRoom_search.setOnClickListener(rightListener) ;
			newsRoom_search.setVisibility(View.VISIBLE) ;
		}
	}
	/**
	 * 显示所有的按钮
	 * @param leftImg
	 * @param alignRightImg
	 * @param rightImg
	 * @param alignRightListener
	 * @param rightListener
	 */
	protected void showAllImage(int leftImg,int alignRightImg,int rightImg,OnClickListener alignRightOnClickListener,OnClickListener rightOnClickListener){
		if(leftImg !=0){
			slide_menu.setImageResource(leftImg) ;
			slide_menu.setOnClickListener(leftDefaultOnClickListener);
			slide_menu.setVisibility(View.VISIBLE);
		}
		if(rightImg !=0){
			newsRoom_search.setImageResource(rightImg) ;
			newsRoom_search.setOnClickListener(rightOnClickListener) ;
			newsRoom_search.setVisibility(View.VISIBLE) ;
		}
		if(alignRightImg !=0){
			fliter.setImageResource(alignRightImg) ;
			fliter.setOnClickListener(alignRightOnClickListener) ;
			fliter.setVisibility(View.VISIBLE) ;
		}
	}

	/**
	 * 隐藏Title左按钮
	 */
	protected void hideTitlebtnLeft() {
		slide_menu.setVisibility(View.GONE);
	}
	/**
	 * 隐藏title右按钮
	 * @Description:TODO
	 */
	protected void hideTitlebtnRight() {
		newsRoom_search.setVisibility(View.GONE) ;
	}

	/**
	 * 隐藏title右按钮
	 * @Description:TODO
	 */
	protected void hideTitlebtnAlignRight() {
		fliter.setVisibility(View.GONE) ;
	}
	/**
	 * 设置内容页面
	 * 
	 * @param view   给一个view
	 */
	protected void setContentViewItem(View view) {
		content_view.addView(view);
	}
	/**
	 * 设置内容页面
	 *  
	 * @param layoutId  给一个xml布局
	 */ 
	protected void setContentViewItem(int layoutId) {
		// 获得一个可扩展的Layout实例
		LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		// 将layout添加到View
		View view = inflater.inflate(layoutId, null);
		content_view.addView(view);
		content_view.setVisibility(View.VISIBLE) ;
	}


	/** 取得内容布局 */
	public LinearLayout getContentView() {
		return content_view;
	}

	/**抽屉菜单的点击事件*/
	private class DrawerItemClickListener implements ListView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			selectItem(position);
		}
	}


	protected void hideCommTitleBar() {
		common_rl.setVisibility(View.GONE) ;
	}

	public RelativeLayout getSearchBar(){
		return search_rl ;
	}

	private void selectItem(int position) {
		switch (position) {
		case 0:
			//			startActivity(new Intent(this,NewsRoomActivity.class));
			//			mDrawerLayout.closeDrawer(rl);
			Intent mainIntent = new Intent(this, NewsRoomActivity.class);
			mainIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
			startActivity(mainIntent);
			mDrawerLayout.closeDrawer(slideMenu_rl);
			overridePendingTransition(0, 0);
			break;
		case 1:
			//			startActivity(new Intent(this,CampaignActivity.class));
			//			mDrawerLayout.closeDrawer(rl);

			Intent mainIntent2 = new Intent(this, CampaignActivity.class);
			mainIntent2.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
			startActivity(mainIntent2);
			mDrawerLayout.closeDrawer(slideMenu_rl);
			overridePendingTransition(0, 0);
			break;
		case 2:
			//			startActivity(new Intent(this,SettingActivity.class));
			//			mDrawerLayout.closeDrawer(rl);

			Intent mainIntent3 = new Intent(this, MyCornerActivity.class);
			mainIntent3.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
			startActivity(mainIntent3);
			mDrawerLayout.closeDrawer(slideMenu_rl);
			overridePendingTransition(0, 0);
			break ;
		case 3:
			//			startActivity(new Intent(this,SettingActivity.class));
			//			mDrawerLayout.closeDrawer(rl);

			Intent mainIntent4 = new Intent(this, QuizActivity.class);
			mainIntent4.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
			startActivity(mainIntent4);
			mDrawerLayout.closeDrawer(slideMenu_rl);
			overridePendingTransition(0, 0);
			break ;
		
		case 4:
			//			startActivity(new Intent(this,SettingActivity.class));
			//			mDrawerLayout.closeDrawer(rl);

			Intent mainIntent5 = new Intent(this, SettingActivity.class);
			mainIntent5.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
			startActivity(mainIntent5);
			mDrawerLayout.closeDrawer(slideMenu_rl);
			overridePendingTransition(0, 0);
			break ;
		
		
		
		
		
		
		default:
			break;
		}
	}

	/***
	 * 默认左按钮的点击事件
	 */
	private View.OnClickListener leftDefaultOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			mDrawerLayout.openDrawer(Gravity.LEFT);
		}
	};

	/**
	 * 隐藏软键盘
	 */
	protected void hideIm() {
		if(CommonUtils.isImeShow(this)){
			CommonUtils.hiddenSoft(this) ;
		}
	}

}
